package com.storage.common.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.io.Serializable;

/**
 * 微信工具类
 *
 * @author hzhang7
 * @date 2019/8/18 2:06 PM
 */
public class WxUtil {



    /**
     * 微信 APP_ID
     */
    public static final String APP_ID = "wx82c75d22a7037a5a";

    /**
     * 微信 APP_SECRET
     */
    public static final String APP_SECRET = "7cff057ad0d10003ffdbfad3432d90d2";

    /**
     * 主动推送信息接口
     */
    public static final String PUBLISH_MSG_URL = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN";

    /**
     * 获取关注用户信息接口
     */
    public static final String USERINFO_URL ="https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";

    /**
     * 获取token接口APP_ID和SECRET替换成微信提供的
     */
    public static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APP_ID&secret=SECRET";

    /**
     * 获取openId接口APP_ID和SECRET替换成微信提供的
     */
    public static final String OPEN_ID_URL = "https://api.weixin.qq.com/sns/jscode2session?appid=APP_ID0&secret=APP_SECRET&js_code=CODE&grant_type=authorization_code";

    /**
     * 获取TICKET，ACCESS_TOKEN替换成缓存中的或者自己获取下
     */
    public static final String TICKET_URL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";

    /**
     * 获取授权用户的基本信息
     */
    public static final String AUTHORIZATION_USERINFO_URL = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPEN_ID&lang=zh_CN";


    @Autowired
    private RestTemplate restTemplate;

    /**
     * 推送消息接口
     * @param accessToken
     * @param json 数据集 "{\"touser\": \"" + 用户的openId + "\",\"msgtype\": \"text\", \"text\": {\"content\": \"" + 内容 + "\"}}";
     */
    public void sendPostText(String accessToken, String json) {
        restTemplate.postForEntity(PUBLISH_MSG_URL.replace("ACCESS_TOKEN", accessToken), json, String.class);
    }

    /**
     * 获取用户信息
     * @param openId 用户的openID
     * @param accessToken
     * @return com.xupx.wxutils.pojo.WxUserInfo 用户的信息，昵称:nickname，用户头像地址照片：headimgurl
     */
    public WxUserInfo getUserInfo(String openId, String accessToken) {
        ResponseEntity<WxUserInfo> responseEntity = restTemplate.getForEntity(USERINFO_URL.replace("ACCESS_TOKEN", accessToken).replace("OPENID", openId), WxUserInfo.class);
        return responseEntity.getBody();
    }

    /**
     * 获取用户信息
     * @param code 微信code
     * @return com.xupx.wxutils.pojo.WxUserInfo 用户的信息，昵称:nickname，用户头像地址照片：headimgurl
     */
    public WxUserInfo getUserInfo(String code) {
        WxOpenId openId = getOpenId(code);
        WxAccessToken accessToken = getAccessToken();
        ResponseEntity<WxUserInfo> responseEntity = restTemplate.getForEntity(USERINFO_URL.replace("ACCESS_TOKEN", accessToken.access_token).replace("OPENID", openId.openid), WxUserInfo.class);
        return responseEntity.getBody();
    }

    /**
     * 获取accessToken
     * @return com.xupx.wxutils.pojo.WxAccessToken
     */
    public WxAccessToken getAccessToken() {
        ResponseEntity<WxAccessToken> responseEntity = restTemplate.getForEntity(TOKEN_URL, WxAccessToken.class);
        return responseEntity.getBody();
    }

    /**
     * 获取用户的openId
     * @param code
     * @return com.xupx.wxutils.pojo.WxOpenId
     */
    public WxOpenId getOpenId(String code) {
        ResponseEntity<WxOpenId> responseEntity = restTemplate.getForEntity(OPEN_ID_URL.replace("CODE", code).replace("APP_ID", APP_ID).replace("APP_SECRET", APP_SECRET), WxOpenId.class);
        return responseEntity.getBody();
    }


    /**
     * 获取ticket
     * @param acessToken
     * @return com.xupx.wxutils.pojo.WxTicket
     */
    public WxTicket getTicket(String acessToken) {
        ResponseEntity<WxTicket> responseEntity = restTemplate.getForEntity(TICKET_URL.replace("ACCESS_TOKEN", acessToken), WxTicket.class);
        return responseEntity.getBody();
    }

    /**
     * 获取授权用户的基本信息
     * @param acessToken
     * @return com.xupx.wxutils.pojo.WxTicket
     */
    public WxUserInfo getAuthorizationUser(String acessToken,String openId) {
        ResponseEntity<WxUserInfo> responseEntity = restTemplate.getForEntity(AUTHORIZATION_USERINFO_URL.replace("ACCESS_TOKEN", acessToken).replace("OPEN_ID",openId), WxUserInfo.class);
        return responseEntity.getBody();
    }

    public class WxUserInfo implements Serializable {
        private String nickname;
        private String headimgurl;

        public String getNickname() {
            return nickname;
        }

        public void setNickname(String nickname) {
            this.nickname = nickname;
        }

        public String getHeadimgurl() {
            return headimgurl;
        }

        public void setHeadimgurl(String headimgurl) {
            this.headimgurl = headimgurl;
        }
    }

    public class WxAccessToken implements Serializable {
        private String access_token;
    }

    public class WxOpenId extends WxAccessToken{
        private String openid;
    }

    public class WxTicket {
        private String ticket;
    }

}
